package com.golddaymicromall.activity.setting.personal.address;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.AddressInfo;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

/**
 * Created by dawd on 2016/3/15.
 */
public class AddressChoiceActivity extends BaseActivity implements AdapterView.OnItemClickListener, View.OnClickListener {
    private ListView mlv;
    private List<AddressInfo> list = new ArrayList<AddressInfo>();


    private String customerid;
    private MyAdapter adapter;
    private Button mnewadd_btn;
    private LinearLayout maddressChoiceBack;
    ProgressBar progress;
    private  TextView  maddresschoice_centerInParent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_choice);
        customerid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        attemptAddress(customerid);
    }

    private void initView() {
        mnewadd_btn = (Button) findViewById(R.id.newadd_btn);
        mlv = (ListView) findViewById(R.id.addressLvchoice);
        maddressChoiceBack = (LinearLayout) findViewById(R.id.addresschoice_back);
        maddresschoice_centerInParent= (TextView) findViewById(R.id.addresschoice_centerInParent);
        maddressChoiceBack.setOnClickListener(this);
        progress = (ProgressBar) findViewById(R.id.progress);
        mlv.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> mlv, View arg1, int arg2, long arg3) {
        Intent intent = new Intent();
        intent.putExtra("id", list.get(arg2).getId());
        intent.putExtra("name_address", list.get(arg2).getReceivername());
        intent.putExtra("address", list.get(arg2).getCountryname() + list.get(arg2).getProvincename() + list.get(arg2).getCityname() + list.get(arg2).getAreaname() + list.get(arg2).getStreet());
        intent.putExtra("phone", list.get(arg2).getPhone());
        setResult(RESULT_OK, intent);
        finish();
    }


    public void attemptAddress(String customerid) {

        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.address.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerId", customerid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(s -> {
                    if (!"".equals(s)) {
                        if (s.contains("errorToken") || "0".equals(s)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(s)) {
                            Toast.makeText(getApplicationContext(), "暂无数据", Toast.LENGTH_SHORT).show();
                            maddresschoice_centerInParent.setVisibility(View.VISIBLE);
                        } else {
                            list = JSON.parseArray(s, AddressInfo.class);
                            adapter = new MyAdapter();
                            mlv.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    showError();
                    throwable.printStackTrace();
                });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.addresschoice_back:
                finish();
                break;
        }
    }



    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder hold;
            if (convertView == null) {
                convertView = View.inflate(AddressChoiceActivity.this, R.layout.item_addresschoice, null);
                hold = new ViewHolder();
                hold.mname_address = (TextView) convertView.findViewById(R.id.name_address);
                hold.mneirongq = (TextView) convertView.findViewById(R.id.address);
                hold.mtelephonenumber = (TextView) convertView.findViewById(R.id.telephonenumber);
                convertView.setTag(hold);
            } else {
                hold = (ViewHolder) convertView.getTag();
            }
            hold.mname_address.setText(list.get(position).getReceivername());
            hold.mneirongq.setText(list.get(position).getCountryname() + list.get(position).getProvincename() + list.get(position).getCityname() + list.get(position).getAreaname() + list.get(position).getStreet());
            hold.mtelephonenumber.setText(list.get(position).getPhone());

            return convertView;
        }

        private class ViewHolder {
            TextView mname_address;
            TextView mneirongq;
            TextView mtelephonenumber;
        }
    }
}
